<?php

class Model_HmPollAnswer extends Zend_Db_Table_Abstract
{
	protected $_name = 'hm_poll_answer';
	protected $_primary = 'id';
	protected $_sequence = true;
	protected $_referenceMap = array(
        'Model_HmPoll' => array(
            'columns'       => array('hm_poll_question_id'),
            'refTableClass' => 'Model_HmPoll',
            'refColumns'    => array('id'),
			'onDelete'      => self::CASCADE
        ));	
	public function getAll($id)
	{
		$row = $this->fetchAll($this->select()->where('hm_poll_question_id = '.(int)$id));
		if(!$row) {
			throw new Exception('Could not fetch records.');
		}
		return $row->toArray();
	}
	
	public function getRow($id)
	{
		$id = (int)$id;
		$row = $this->fetchRow('id = '.(int)$id);
		if (!$row) {
			throw new Exception('Could not find row '.(int)$id);
		}
		return $row->toArray();
	}
	
	public function addRow($data)
	{
		$row = $this->createRow($data);
		$row->save();
	}
	
	public function updateRow($id, $data)
	{
		$where = 'id = '.(int)$id;
		$row = $this->update($data, $where);
		if(!$row) {
			throw new Exception('Could not update row '.(int)$id);
		}
		return true;
	}
	
	public function deleteRow($id)
	{
		$where = 'id = '.(int)$id;
		$row = $this->delete($where);
		if(!$row) {
			throw new Exception('Could not delete row '.(int)$id);
		}
		return true;
	}
	
	public function assignVote($id)
	{
		$row = $this->getRow((int)$id);
		$data = array('votes'=>$row['votes'] + 1);
		$update = $this->updateRow((int)$id, $data);
	}
}